home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.3 / object.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-29  |  3.0 KB  |  104 lines

  1. #include "object.h"
  2.  
  3. std::vector<MESH*> objectMeshes;
  4.  
  5. HRESULT LoadObjectResources(IDirect3DDevice9* Device)
  6. {
  7.     MESH *dragon = new MESH("objects/dragon.x", Device);
  8.     objectMeshes.push_back(dragon);
  9.  
  10.     MESH *f1 = new MESH("objects/footman01.x", Device);
  11.     objectMeshes.push_back(f1);
  12.  
  13.     MESH *ring = new MESH("objects/ring.x", Device);
  14.     objectMeshes.push_back(ring);
  15.  
  16.     return S_OK;
  17. }
  18.  
  19. void UnloadObjectResources()
  20. {
  21.     for(int i=0;i<objectMeshes.size();i++)
  22.         objectMeshes[i]->Release();
  23.  
  24.     objectMeshes.clear();
  25. }
  26.  
  27. //////////////////////////////////////////////////////////////////////////////
  28. //                            OBJECT CLASS                                    //
  29. //////////////////////////////////////////////////////////////////////////////
  30.  
  31. OBJECT::OBJECT()
  32. {
  33.     m_type = 0;
  34. }
  35.  
  36. OBJECT::OBJECT(int t, D3DXVECTOR3 pos, D3DXVECTOR3 rot, D3DXVECTOR3 sca)
  37. {
  38.     m_type = t;
  39.     m_meshInstance.SetPosition(pos);
  40.     m_meshInstance.SetRotation(rot);
  41.     m_meshInstance.SetScale(sca);
  42.     m_meshInstance.SetMesh(objectMeshes[m_type]);
  43.     m_boxMesh = NULL;
  44.     m_sphereMesh = NULL;
  45.  
  46.     if(m_type == DRAGON)
  47.         m_name = "Dragon";
  48.     else if(m_type == BOB)
  49.         m_name = "Brave Bob";
  50.     else if(m_type == RING)
  51.         m_name = "";
  52.  
  53.     m_BBox = m_meshInstance.GetBoundingBox();
  54.     m_BSphere = m_meshInstance.GetBoundingSphere();
  55.  
  56.     D3DXCreateBox(m_meshInstance.m_pMesh->m_pDevice, m_BBox.max.x - m_BBox.min.x, 
  57.                                       m_BBox.max.y - m_BBox.min.y,
  58.                                       m_BBox.max.z - m_BBox.min.z,
  59.                                       &m_boxMesh, NULL);
  60.  
  61.     D3DXCreateSphere(m_meshInstance.m_pMesh->m_pDevice, m_BSphere.radius, 20, 20, &m_sphereMesh, NULL);
  62. }
  63.  
  64. void OBJECT::Render()
  65. {
  66.     m_meshInstance.Render();
  67. }
  68.  
  69. void OBJECT::RenderBoundingVolume(int typ)
  70. {
  71.     D3DXMATRIX world;
  72.  
  73.     D3DMATERIAL9 mtrl;
  74.     mtrl.Diffuse = D3DXCOLOR(0.5f, 0.5f, 1.0f, 0.1f);
  75.     mtrl.Ambient = D3DXCOLOR(0.3f, 0.3f, 0.3f, 0.1f);
  76.     mtrl.Emissive = mtrl.Specular = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
  77.     mtrl.Power = 0.2f;
  78.  
  79.     m_meshInstance.m_pMesh->m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
  80.     m_meshInstance.m_pMesh->m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  81.  
  82.     m_meshInstance.m_pMesh->m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
  83.     m_meshInstance.m_pMesh->m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR);
  84.  
  85.     m_meshInstance.m_pMesh->m_pDevice->SetMaterial(&mtrl);
  86.     m_meshInstance.m_pMesh->m_pDevice->SetTexture(0, NULL);
  87.     m_meshInstance.m_pMesh->m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
  88.  
  89.     if(typ == 1)
  90.     {
  91.         D3DXVECTOR3 center = (m_BBox.max + m_BBox.min) / 2.0f;
  92.         D3DXMatrixTranslation(&world, center.x, center.y, center.z);
  93.         m_meshInstance.m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &world);                
  94.         m_boxMesh->DrawSubset(0);
  95.     }
  96.     else if(typ == 2)
  97.     {
  98.         D3DXMatrixTranslation(&world, m_BSphere.center.x, m_BSphere.center.y, m_BSphere.center.z);
  99.         m_meshInstance.m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &world);                
  100.         m_sphereMesh->DrawSubset(0);
  101.     }
  102.  
  103.     m_meshInstance.m_pMesh->m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  104. }